/*
v 1.3
the ThrowObj Validation Alert Plugin 

- require jquery UI
- by tiger

- 1.3 scroll to last message [require set propoties formParent:"selector"]
- 1.2 add Topic to error obj

use : $("#holder").objValidate({throwObj:valMes}); throw data from OBJ
use : $("#holder").objValidate({returnTo:"abc()"}); for Call back

obj Receive : {"Status":"Not Pass","Errors":[{"Key":"Name","Message":"0-8 char","Topic":"Your first name"},{"Key":"E-mail","Message":"0-8 char"}

*/

(function($) {

    jQuery.fn.objValidate = function(settings) {
        var defaults = { throwObj: { "Status": "Complete" }, returnTo: null, removeAble: false,	formParent:".ui-layout-center:last" }; //removeAble : true, false, 'auto'
        var options = jQuery.extend(defaults, settings);

        $("body").find('div[keyof="status"]').remove(); //remove old
        $("body").find('div[keyof="Errors"]').remove();
        //Status
        $(this).after('<div keyof="status" class="ui-state-highlight" style="padding:4px; cursor:pointer; margin-top:2px"><div class="ui-icon ui-icon-info"></div><div class="message">' + options.throwObj.Status + '</div></div>');
        $(this).next("div").find("div.ui-icon").css("float", "left");

        $(this).next("div").hide().fadeIn(1000,function(){
			//call back function
			if (options.returnTo != null && $("body").find('div[keyof="Errors"]').size() == 0) {
				setTimeout(options.returnTo, 0);
			} 
			
			//removeable
			if (options.removeAble == "auto") {
				$(this).fadeOut(3000,function(){
					$(this).remove();
				});
			} else if (options.removeAble == true) {
				$(this).click(function() {
					$(this).remove();
				});
			}
			//scroll to
			$(options.formParent).scrollTop($("[keyof]:last").offset().top);
		});
		//$(document).scrollTop(document.body.scrollHeight);
        //end status
        //Error
        if (options.throwObj.Errors) {
            var Selector = $(this).next('div.ui-state-highlight');
            $(options.throwObj.Errors).each(function(i) {
                $("body").find('div#' + options.throwObj.Errors[i].Key + i).remove(); //remove old
                //each error
                //Selector.after('<div keyof="Errors" id="'+ options.throwObj.Errors[i].Key + i +'" class="ui-state-error" style="padding:4px; cursor:pointer; margin-top:2px"><div class="ui-icon ui-icon-alert"></div><div class="message">'+options.throwObj.Errors[i].Key+" : "+options.throwObj.Errors[i].Message+'</div></div>');
                Selector.after('<div keyof="Errors" id="' + options.throwObj.Errors[i].Key + i + '" class="ui-state-error" style="padding:4px; cursor:pointer; margin-top:2px"><div class="ui-icon ui-icon-alert"></div><div class="message">' + options.throwObj.Errors[i].Message + '</div></div>');
                $('div#' + options.throwObj.Errors[i].Key + i).find("div.ui-icon").css("float", "left");
                $('div#' + options.throwObj.Errors[i].Key + i).hide().fadeIn(1000);
                $('div#' + options.throwObj.Errors[i].Key + i).click(function() {
                    $("#" + options.throwObj.Errors[i].Key).focus();
                    //removeable
                    if (options.removeAble == true) {
                        $(this).remove();
                    }
                });
                if (options.removeAble == "auto") {
                    $('div#' + options.throwObj.Errors[i].Key + i).fadeOut(3000,function(){$(this).remove()});
                }
				
            });
        }
        //end Error
		//alert($("[keyof]:last").offset().top);

    }

})(jQuery);